package com.ruoyi.material.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.material.domain.BaseMaterial;
import com.ruoyi.material.domain.VimsImageInfo;
import com.ruoyi.material.domain.constant.FileConstant;
import com.ruoyi.material.domain.vo.ImageVo;
import com.ruoyi.material.service.IBaseMaterialService;
import com.ruoyi.material.service.IVIMSFileService;
import com.ruoyi.material.service.IVimsImageInfoService;
import com.ruoyi.material.util.FileUtil;
import com.ruoyi.material.util.RandomlyGeneratedUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: MSS
 * @description: VIMS对接文件服务
 * @author: cyw
 * @create: 2021-07-27 15:48
 **/
@Service
public class VIMSFileService implements IVIMSFileService {
    Logger log = LoggerFactory.getLogger(VIMSFileService.class);
    @Autowired
    private IBaseMaterialService baseMaterialService;
    @Autowired
    private IVimsImageInfoService vimsImageInfoService;

    /**
     * 截图管理 把截图记录保存到数据库和本地
     */
    public boolean saveImageFiles(List<ImageVo> images){
        if (images == null){
            return false;
        }
        for (ImageVo vo:images){
            saveImageFile(vo);
        }
        return true;
    }
    public boolean saveImageFile(ImageVo vo) {
        VimsImageInfo image = vo.transferToVimsImageInfo();
        String path = appendDataToLocal(image.getSaveDate(),vo.getCaptureTime());
        log.debug("数据库中的目录 {}" ,path );
        try {
            String pcode = baseMaterialService.createMaterialByFilePath(path,"20211615",vo.getMissionName());
            saveDataToDatabase(image,vo.getMissionName(),pcode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean saveImageFile(String imgUrl, String channelName, String channelCode, Date recivedData,String missionName) {
        VimsImageInfo image = new VimsImageInfo(null,null,imgUrl,channelName,channelCode);
        image.setCreateBy(missionName);
        image.setCreateTime(recivedData);
        image.setCreateTime(recivedData);
        String path = appendDataToLocal(image.getSaveDate(),recivedData);
        log.debug("数据库中的目录 {}" ,path );
        try {
            String pcode = baseMaterialService.createMaterialByFilePath(path,"20211615",missionName);

            saveDataToDatabase(image,missionName,pcode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 在本地保存数据
     * @param data 保存的数据
     * return 时间路径
     */
    private String appendDataToLocal(String data,Date recivedData){
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        String dirPath = RuoYiConfig.getUploadPath() + FileConstant.VIMS_DATA_PATH+df.format(recivedData);
        String res = dirPath.substring(dirPath.lastIndexOf(FileConstant.VIMS_DATA_PATH),dirPath.length());
        //创建文件夹
        if (!new File(dirPath).exists()){
            FileUtil.createDir(dirPath);
        }
        // 更加优雅的 todo  通过 xml 的形式
        String filePath = dirPath+"/"+FileConstant.VIMS_DATA_TYPE;
        if (!new File(filePath).exists()){
            FileUtil.createFile(filePath);
        }
        //文本中追加内容
        try {
            FileUtil.addContent(filePath,data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.debug("数据保存的本地路径 {}" ,filePath );
        return res;
    }
    /**
     * 把记录保存到数据库
     */
    private boolean saveDataToDatabase(VimsImageInfo image ,String createBy,String pcode){
        String fileName = RandomlyGeneratedUtil.GeneratID(FileConstant.VIMS_PREFIX,image.getChannelName(),createBy);
        image.setId(RandomlyGeneratedUtil.GeneratID());
        String id = baseMaterialService.insertFileRecord(fileName,FileConstant.VIMS_IMGAGE_TYPE,0L,pcode,createBy,image.getImgUrl(),image.getCreateTime());
        BaseMaterial material = baseMaterialService.selectBaseMaterialById(id);
        image.setFileCode(""+material.getFileCode());
        vimsImageInfoService.insertVimsImageInfo(image);
        return true;
    }
}
